<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title>sign in</title>
	<link rel="stylesheet"
		href="../static/assets/bootstrap/css/bootstrap.min.css"
		th:href="@{/assets/bootstrap/css/bootstrap.min.css}" />
    <link rel="stylesheet"
          href="../static/assets/tianai/styles/tac.css"
          th:href="@{/assets/tianai/styles/tac.css}" />
</head>

<body>
	<form id="signInForm" action="/security_check" method="post">
		<div class="container mt-5">
			<div class="row justify-content-center">
				<div class="col-sm-3">
					<div class="form-group row my-2 px-2 justify-content-center">
						<input type="hidden" name="graphId" id="graphId" />
						<h3 class="text-center">登录</h3>
						<p class="text-center alert alert-danger" th:if="${error!=null && error!=''}" th:text="${error}"></p>
					</div>

					<div class="form-group row my-2 px-2 justify-content-center">
						<input type="text" name="username" class="form-control"
							   id="username" required="required" placeholder="用户名" />
					</div>
					<div class="form-group row my-2 px-2 justify-content-center">
						<input type="password" name="password" class="form-control"
							   id="password" required="required" placeholder="密　码" />
					</div>
                    <div id="captcha-box"></div>
					<div class="form-group row my-2 px-2">
						<div class="px-0">
							<!--<div class="main-checkbox">
								<input type="checkbox" id="rememberUsername" name="check" /> <label
									for="rememberUsername">记住用户名</label>
							</div>-->
							<div class="my-2 main-checkbox">
								<button type="submit" class="btn btn-primary">登录</button>
								<a class="btn btn-sm" href="/signUp">注册</a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>


	<script src="../static/assets/jquery.min.js"
		th:src="@{/assets/jquery.min.js}"></script>
	<script src="../static/assets/bootstrap/js/bootstrap.min.js"
		th:src="@{/assets/bootstrap/js/bootstrap.min.js}"></script>
	<script th:src="@{/assets/localforage.min.js}"
		src="../static/assets/localforage.min.js"></script>
    <script src="../static//assets/tianai/tac.min.js"
            th:src="@{/assets/tianai/tac.min.js}"></script>
	<script th:inline="javascript">
    /*<![CDATA[*/
    	var captchaPassed = false;
        $(document).ready(function () {

            const config = {
               // 生成接口 (必选项,必须配置, 要符合tianai-captcha默认验证码生成接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
               requestCaptchaDataUrl: "/captcha/gen",
               // 验证接口 (必选项,必须配置, 要符合tianai-captcha默认验证码校验接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
               validCaptchaUrl: "/captcha/check",
               // 验证码绑定的div块 (必选项,必须配置)
               bindEl: "#captcha-box",
               // 验证成功回调函数(必选项,必须配置)
               validSuccess: (res, c, tac) => {
                   // todo 这里res为后端返回的数据，一般返回验证成功的token， 进行登录或其它操作时，要把返回的token传给后端进行二次验证
                   console.log("验证成功，后端返回的数据为", res);
                  // 调用登录方法进行登录 这里login登录方法为自己的登录函数
                  //   this.login(res.captchaToken)
                 captchaPassed = true;
                 $("#signInForm").submit();
                   // 销毁验证码服务
                 tac.destroyWindow();
               },
               // 验证失败的回调函数(可忽略，如果不自定义 validFail 方法时，会使用默认的)
               validFail: (res, c, tac) => {
                   // 验证失败后重新拉取验证码
                   tac.reloadCaptcha();
               }
            }

            localforage.getItem('username').then(function (value) {
                // This code runs once the value has been loaded
                // from the offline store.
                if (value != null && value != "") {
                    $("#username").val(value);
                    $("#rememberUsername").prop("checked", "checked");
                    $("#password").focus();
                }

            }).catch(function (err) {
                // This code runs if there were any errors
                console.log(err);
            });

            $("#signInForm").on("submit", function () {

                if(captchaPassed == false){
                    event.preventDefault();
                    // 创建 TAC 启动验证码服务,调用该方法后会在指定的div块中渲染出验证码
                    new TAC(config).init();
                }


                if ($("#rememberUsername").prop("checked")) {
                    localforage.setItem('username', $("#username").val()).then(function (value) {
                        // Do other things once the value has been saved.
                    }).catch(function (err) {
                        // This code runs if there were any errors
                        console.log(err);
                    });
                } else {
                    localforage.removeItem('username').then(function (value) {
                        // Do other things once the value has been saved.
                    }).catch(function (err) {
                        // This code runs if there were any errors
                        console.log(err);
                    });
                }

                $(this).parents("#signInForm").submit();
            })
        });
    /*]]>*/
</script>

</body>
</html>
